package just.stuff.morsels.objects;

import java.io.Serializable;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

import com.sleepycat.persist.model.DeleteAction;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.Relationship;
import com.sleepycat.persist.model.SecondaryKey;

@Entity(version=2)
public class User implements Serializable {

	@PrimaryKey
	private String id;
	@SecondaryKey( relate=Relationship.ONE_TO_ONE)
	private String email;
	
	@SecondaryKey(relate=Relationship.MANY_TO_MANY, relatedEntity=Label.class, onRelatedEntityDelete=DeleteAction.NULLIFY)
	private Set<String> assistanceSubscriptions;
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public Set<String> getAssistanceSubscriptions() {
		if( assistanceSubscriptions == null ) {
			return Collections.emptySet();
		}
		return Collections.unmodifiableSet( assistanceSubscriptions );
	}

	public void addAssistanceSubscription( String label ) {
		synchronized( this ) {
			if( assistanceSubscriptions == null ) {
				assistanceSubscriptions = new TreeSet<String>();
			}
		}
		assistanceSubscriptions.add( label );
	}
	
	public void removeAssistanceSubscription( String label ) {
		if( assistanceSubscriptions != null ) {
			assistanceSubscriptions.remove( label );
		}
	}
	
	public boolean isAssistanceSubscribed( String label ) {
		return getAssistanceSubscriptions().contains( label );
	}
	
	public void toggleAssistanceSubscription( String label ) {
		if( isAssistanceSubscribed( label) ) {
			removeAssistanceSubscription( label );
		} else {
			addAssistanceSubscription( label );
		}
	}
	
	public void setAssistanceSubscriptions(Set<String> assistanceSubscriptions) {
		this.assistanceSubscriptions = assistanceSubscriptions;
	}
	
	
	
	
	
}
